package com.heima.behavior.controller.v1;

import com.heima.behavior.service.*;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.*;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/v1")
@Api(tags = "用户行为管理api", value = "用户行为管理api")
public class ApUserBehaviorController {
    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    private ApLikesBehaviorService apLikesBehaviorService;
    @Autowired
    private ApReadBehaviorService apReadBehaviorService;
    @Autowired
    private ApUnlikeBehaviorService apUnlikeBehaviorService;
    @Autowired
    private ApCollectionBehaviorService apCollectionBehaviorService;
    @Autowired
    private ApArticleBehaviorService apArticleBehaviorService;

    //点赞行为记录
    @PostMapping("/likes_behavior")
    @ApiOperation("点赞行为记录")
    public ResponseResult like(@RequestBody @Validated LikesBehaviorDTO dto) {
        //获取当前登录用户
        Integer userId = AppThreadLocalUtils.getUser().getId();
        //为空,未登录,禁止点赞
        if (userId == null) {
            //未登录
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
        }
        //获取行为实体
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(userId, dto.getEquipmentId());
        //将返回的行为实体对象的id赋值到dto
        dto.setEquipmentId(apBehaviorEntry.getEntryId());
        //调用点赞行为记录方法
        return apLikesBehaviorService.like(dto);
    }

    //阅读行为记录
    @PostMapping("/read_behavior")
    @ApiOperation("阅读行为记录")
    public ResponseResult read(@RequestBody @Validated ReadBehaviorDTO dto) {
        //获取当前登录用户
        Integer userId = AppThreadLocalUtils.getUser().getId();
        //获取行为实体
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(userId, dto.getEquipmentId());
        //非空判断
        if (apBehaviorEntry == null) {
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN, "请登录或者使用设备进行阅读!");
        }
        //将返回的行为实体对象的id赋值到dto
        dto.setEquipmentId(apBehaviorEntry.getEntryId());
        //调用阅读行为记录方法
        return apReadBehaviorService.readBehavior(dto);
    }

    //不喜欢行为记录
    @PostMapping("un_likes_behavior")
    @ApiOperation("不喜欢行为记录")
    public ResponseResult unlikes(@RequestBody UnLikesBehaviorDTO dto) {
        //获取当前登录用户
        Integer userId = AppThreadLocalUtils.getUser().getId();
        //为空,未登录,禁止调用不喜欢行为记录方法
        if (userId == null) {
            //未登录
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
        }
        //获取行为实体
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(userId, dto.getEquipmentId());
        //将返回的行为实体对象的id赋值到dto
        dto.setEquipmentId(apBehaviorEntry.getEntryId());
        //调用不喜欢行为记录方法
        return apUnlikeBehaviorService.unlikeBehavior(dto);
    }

    //收藏行为记录
    @PostMapping("collection_behavior")
    @ApiOperation("收藏行为记录")
    public ResponseResult collection(@RequestBody CollectionBehaviorDTO dto) {
        //获取当前登录用户
        Integer userId = AppThreadLocalUtils.getUser().getId();
        //非空判断
        if (userId == null) {
            //未登录
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
        }
        //获取行为实体
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(userId, dto.getEquipmentId());
        //将返回的行为实体对象的id赋值到dto
        dto.setEquipmentId(apBehaviorEntry.getEntryId());
        //调用收藏行为记录方法
        return apCollectionBehaviorService.collectBehavior(dto);
    }

    //加载文章详情 数据回显
    @PostMapping("/article/load_article_behavior")
    @ApiOperation("加载文章详情 数据回显")
    public ResponseResult load(@RequestBody ArticleBehaviorDTO dto) {
        //调用方法
        return apArticleBehaviorService.loadArticleBehavior(dto);
    }
}